<?php
namespace app\api\controller;
use think\facade\Db;
use think\facade\Request;
class Submission
{
    public function submissionList()
    {
        $pageData = Request::param();
        $page = $pageData['page'] ?? 1;
        $perPage = $pageData['pageSize'] ?? 10;
        $user_id = $pageData['user_id'] ?? '';
        $problem_id = $pageData['problem_id'] ?? '';
        $status = $pageData['status'] ?? '';
        $language = $pageData['language'] ?? '';
        $keyword = $pageData['keyword'] ?? '';

        $query = Db::name('submission')->alias('s')
                ->join('user u', 's.user_id = u.id', 'left')
                ->join('problem p', 's.problem_id = p.id', 'left')
                ->field('s.*, u.username, u.student_id, p.title as problem_title, p.difficulty');

        if ($user_id) {
            $query = $query->where('s.user_id', $user_id);
        }
        if ($problem_id) {
            $query = $query->where('s.problem_id', $problem_id);
        }
        if ($status !== '') {
            $query = $query->where('s.status', $status);
        }
        if ($language) {
            $query = $query->where('s.language', $language);
        }
        if ($keyword) {
            $query = $query->where('u.username|u.student_id|p.title', 'like', '%'.$keyword.'%');
        }

        $data = $query->order('s.create_time', 'desc')
                ->paginate([
                    'list_rows' => $perPage,
                    'page'      => $page,
                ]);

        if (!$data) {
            return json([
                'message' => '没有数据', 
                'data' => '', 
                'code' => 200
            ]);
        } else {
            return json([
                'message' => '数据查询成功',
                'code'    => 200,
                'data'    => $data,
            ]);
        }
    }
}